Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Undefined variable</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Undefined_variable"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Undefined_variable rootpage-Undefined_variable skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Undefined variable</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>An <b>undefined variable</b> in the <a href="Source_code" title="Source code">source code</a> of a <a href="Computer_program" title="Computer program">computer program</a> is a <a href="Variable_(programming)" class="mw-redirect" title="Variable (programming)">variable</a> that is accessed in the code but has not been <a href="Declaration_(computer_science)" class="mw-redirect" title="Declaration (computer science)">declared</a> by that code.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p><p>In some <a href="Programming_languages" class="mw-redirect" title="Programming languages">programming languages</a>, an implicit declaration is provided the first time such a variable is encountered at <a href="Compile_time" title="Compile time">compile time</a>. In other languages such a usage is considered to be sufficiently serious that a diagnostic being issued and the compilation fails.
</p><p>Some language definitions initially used the implicit declaration behavior and as they matured provided an option to disable it (e.g. <a href="Perl" title="Perl">Perl</a>'s "<code>use warnings</code>" or <a href="Visual_Basic" title="Visual Basic">Visual Basic</a>'s "<code>Option Explicit</code>").
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<p>The following provides some examples of how various programming language implementations respond to undefined variables. Each code snippet is followed by an error message (if any).
</p>
<div class="mw-heading mw-heading3"><h3 id="CLISP"><a href="CLISP" title="CLISP">CLISP</a></h3></div>
<div class="mw-highlight mw-highlight-lang-lisp mw-content-ltr" dir="ltr"><pre><span class="p">(</span><span class="nb">setf</span><span class="w"> </span><span class="nv">y</span><span class="w"> </span><span class="nv">x</span><span class="p">)</span>
</pre></div>
<pre>*** - EVAL: variable X has no value
</pre>
<div class="mw-heading mw-heading3"><h3 id="C"><a href="C_(programming_language)" title="C (programming language)">C</a></h3></div>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<pre>foo.c: In function `main':
foo.c:2: error: `x' undeclared (first use in this function)
foo.c:2: error: (Each undeclared identifier is reported only once
foo.c:2: error: for each function it appears in.)
</pre>
<div class="mw-heading mw-heading3"><h3 id="JavaScript"><a href="JavaScript" title="JavaScript">JavaScript</a></h3></div>
<p>A ReferenceError only happens if the same piece of executed code has a <code class="mw-highlight mw-highlight-lang-js mw-content-ltr" style="" dir="ltr"><span class="kd">let</span></code> or a <code class="mw-highlight mw-highlight-lang-js mw-content-ltr" style="" dir="ltr"><span class="kd">const</span></code> (but not <code class="mw-highlight mw-highlight-lang-js mw-content-ltr" style="" dir="ltr"><span class="kd">var</span></code>) declaration later on, or if the code is executed in strict mode. In all other cases, the variable will have the special value <code class="mw-highlight mw-highlight-lang-js mw-content-ltr" style="" dir="ltr"><span class="kc">undefined</span></code>.
</p>
<div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span class="s2">"use strict"</span><span class="p">;</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">;</span>
</pre></div>
<div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span class="kd">let</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">;</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// causes error on line 1</span>
</pre></div>
<pre> ReferenceError: x is not defined
Source File: file:///c:/temp/foo.js
</pre>
<div class="mw-heading mw-heading3"><h3 id="Lua"><a href="Lua_(programming_language)" class="mw-redirect" title="Lua (programming language)">Lua</a></h3></div>
<div class="mw-highlight mw-highlight-lang-lua mw-content-ltr" dir="ltr"><pre><span class="n">y</span> <span class="o">=</span> <span class="n">x</span>
</pre></div>
<p>(no error, continuing)
</p>
<div class="mw-highlight mw-highlight-lang-lua mw-content-ltr" dir="ltr"><pre><span class="nb">print</span><span class="p">(</span><span class="nv">y</span><span class="p">)</span>
</pre></div>
<pre>nil
</pre>
<div class="mw-heading mw-heading3"><h3 id="ML_(Standard_ML_of_New_Jersey)"><a href="Standard_ML" title="Standard ML">ML</a> (Standard ML of New Jersey)</h3></div>
<div class="mw-highlight mw-highlight-lang-sml mw-content-ltr" dir="ltr"><pre><span class="kr">val</span> <span class="nv">y</span> <span class="p">=</span> <span class="n">x</span><span class="p">;</span>
</pre></div>
<pre>stdIn:1.9 Error: unbound variable or constructor: x
</pre>
<div class="mw-heading mw-heading3"><h3 id="MUMPS"><a href="MUMPS" title="MUMPS">MUMPS</a></h3></div>
<pre>Set Y=X
</pre>
<pre>&lt;UNDEF&gt;
</pre>
<div class="mw-heading mw-heading3"><h3 id="OCaml"><a href="OCaml" title="OCaml">OCaml</a></h3></div>
<div class="mw-highlight mw-highlight-lang-ocaml mw-content-ltr" dir="ltr"><pre><span class="k">let</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">;;</span>
</pre></div>
<pre>Unbound value x
</pre>
<div class="mw-heading mw-heading3"><h3 id="Perl"><a href="Perl" title="Perl">Perl</a></h3></div>
<div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span class="k">my</span><span class="w"> </span><span class="nv">$y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nv">$x</span><span class="w"> </span><span class="sr">//</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1"># defined-or operator</span>
</pre></div>
<pre>(no error)
</pre>
<div class="mw-heading mw-heading3"><h3 id="PHP_5"><a href="PHP" title="PHP">PHP</a> 5</h3></div>
<div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span class="nv">$y</span> <span class="o">=</span> <span class="nv">$x</span><span class="p">;</span>
</pre></div>
<pre>(no error)
</pre>
<div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span class="nv">$y</span><span class="o">=</span><span class="s2">""</span><span class="p">;</span>
<span class="nv">$x</span><span class="o">=</span><span class="s2">""</span><span class="p">;</span>
<span class="nb">error_reporting</span><span class="p">(</span><span class="k">E_ALL</span><span class="p">);</span>
<span class="nv">$y</span> <span class="o">=</span> <span class="nv">$x</span><span class="p">;</span>
</pre></div>
<pre>PHP Notice: Undefined variable: x in foo.php on line 3
</pre>
<div class="mw-heading mw-heading3"><h3 id="Python"><a href="Python_(programming_language)" title="Python (programming language)">Python</a></h3></div>
<div class="mw-heading mw-heading4"><h4 id="Python_3">Python 3</h4></div>
<div class="mw-highlight mw-highlight-lang-python3 mw-content-ltr" dir="ltr"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">y</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s2">"&lt;pyshell#0&gt;"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">y</span>
<span class="ne">NameError</span><span class="p">:</span> <span class="n">name</span> <span class="s1">'y'</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">defined</span>
</pre></div>
<div class="mw-heading mw-heading4"><h4 id="Python_2.4">Python 2.4</h4></div>
<div class="mw-highlight mw-highlight-lang-pycon mw-content-ltr" dir="ltr"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">y</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"&lt;stdin&gt;"</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">NameError</span>: <span class="n">name 'y' is not defined</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="REXX"><a href="REXX" class="mw-redirect" title="REXX">REXX</a></h3></div>
<div class="mw-highlight mw-highlight-lang-rexx mw-content-ltr" dir="ltr"><pre><span class="kr">signal</span><span class="w"> </span><span class="kr">on</span><span class="w"> </span>novalue
y<span class="w"> </span><span class="o">=</span><span class="w"> </span>x
</pre></div>
<pre>+++ Error 30 in line 2: Label not found
</pre>
<div class="mw-heading mw-heading3"><h3 id="Ruby"><a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a></h3></div>
<div class="mw-highlight mw-highlight-lang-irb mw-content-ltr" dir="ltr"><pre><span class="gp">irb(main):001:0&gt; </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span>
<span class="go">NameError: undefined local variable or method `x' for main:Object</span>
<span class="go"> from (irb):1</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Tcl"><a href="Tcl" title="Tcl">Tcl</a></h3></div>
<div class="mw-highlight mw-highlight-lang-tcl mw-content-ltr" dir="ltr"><pre><span class="o">%</span><span class="w"> </span><span class="k">set</span><span class="w"> </span>y<span class="w"> </span><span class="nv">$x</span>
<span class="nv">can</span><span class="err">'</span>t<span class="w"> </span>read<span class="w"> </span><span class="s2">"x"</span><span class="o">:</span><span class="w"> </span>no<span class="w"> </span>such<span class="w"> </span>variable
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="VBScript"><a href="VBScript" title="VBScript">VBScript</a></h3></div>
<div class="mw-highlight mw-highlight-lang-vbscript mw-content-ltr" dir="ltr"><pre><span class="kd">Dim</span><span class="w"> </span><span class="nv">y</span>
<span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span>
</pre></div>
<pre>(no error)
</pre>
<div class="mw-highlight mw-highlight-lang-vbscript mw-content-ltr" dir="ltr"><pre><span class="k">Option</span><span class="w"> </span><span class="k">Explicit</span>

<span class="kd">Dim</span><span class="w"> </span><span class="nv">y</span>
<span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span>
</pre></div>
<pre>(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'
</pre>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text">"undefined variable." YourDictionary, n.d. Web. 24 July 2013. &lt;<a rel="nofollow" class="external free" href="http://computer.yourdictionary.com/undefined-variable">http://computer.yourdictionary.com/undefined-variable</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20130415073925/http://computer.yourdictionary.com/undefined-variable">Archived</a> 2013-04-15 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a>&gt;.</span>
</li>
</ol></div></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-29" href="https://en.wikipedia.org/wiki/?title=Undefined_variable&amp;oldid=1303097621">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>